#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <mysql.h>
#include "user.h"
#include "finance.h"
using namespace std;

void login(int select);   //登录函数
void adminmenu();         //管理员界面
void usermenu(int id);    //用户界面
void managemenu(int id);  //财务信息管理界面
void datamenu(int id);    //数据管理界面
void alterpasswd(int id); //修改密码函数
int main()        //主函数
{
    int select=0; //定义选择变量
    do            //do...while实现界面
    {
        cout << "\n\t\t===================================" << endl;
        cout << "\t\t*        专 业 理 财 系 统        *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*         (1) 管理员登录          *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*         (2) 用户登录            *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*         (0) 退 出               *" << endl;
        cout << "\t\t===================================" << endl;
        cout << "\t\t请输入您的选择: ";
        cin >> select;
        switch (select)
        {
            case 0:
                break;
            case 1:
                login(1);
                break;
            case 2:
                login(2);
                break;
            default :
                system("cls");    //清屏语句
                cout<<"\t\t输入错误请重新输入！"<<endl;
        }
    }
    while(select!=0);
    return 0;
}
void login(int select)    //登录函数
{
    int id;
    char passwd[20];
    char str[100];        //存放SQL语句
    cout << "请输入账号：";
    cin >> id;
    cout << "请输入密码：";
    cin >> passwd;
    MYSQL mysql;          //定义MYSQL数据类型结构的句柄
    mysql_init(&mysql);   //初始化
    MYSQL_RES *result;    //存储查询的结果集合
    if(mysql_real_connect(&mysql,"localhost","root","123456","financialmanage",3306,NULL,0)!=NULL)  //判断数据库连接是否成功
    {
        mysql_query(&mysql,"SET NAMES GBK");    //设置编码格式
        if(select==1)
            sprintf(str,"select * from %s where (id = %d and passwd = '%s')","adminInformation",id,passwd);   //将SQL语句格式化复制给str
        else
            sprintf(str,"select * from %s where (id = %d and passwd = '%s')","userInformation",id,passwd);
        if( mysql_real_query(&mysql,str,(unsigned int)strlen(str))==0)    //判断查询信息是否成功，成功返回0
        {
            result = mysql_store_result(&mysql);    //接收查询结果集合
            if (mysql_num_rows(result)!=0)          //判断结果集合是否为空
            {
                system("cls");
                cout << "登录成功！" << endl;
                if(select==1)
                    adminmenu();
                else
                    usermenu(id);
            }
            else
            {
                cout << "登录失败！" << endl;
            }
        }
    }
    else
        cout << "数据库连接失败!" << endl;
    if(result!=NULL) mysql_free_result(result);    //释放结果资源
    mysql_close(&mysql);    //关闭数据库连接
}
void adminmenu()   //管理员界面
{
    int select=0;     //定义选择变量
    user userTemp;    //定义user类对象
    do                //do...while实现界面
    {
        cout << "\n\t\t===================================" << endl;
        cout << "\t\t*        管 理 员 界 面           *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*       (1) 增加用户信息          *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*       (2) 删除用户信息          *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*       (3) 查看用户信息          *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*       (4) 修改用户信息          *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*       (0) 退 出                 *" << endl;
        cout << "\t\t===================================" << endl;
        cout << "\t\t请输入您的选择: ";
        cin >> select;
        switch (select)
        {
            case 0:
                break;
            case 1:
                userTemp.insertInfo();
                break;
            case 2:
                userTemp.deleteInfo();
                break;
            case 3:
                userTemp.selectInfo();
                break;
            case 4:
                userTemp.updateInfo();
                break;
            default :
                system("cls");    //清屏语句
                cout<<"\t\t输入错误请重新输入！"<<endl;
        }
    }
    while(select!=0);
    system("cls");
}
void usermenu(int id)    //用户界面
{
    int select=0;
    do
    {
        cout << "\n\t\t===================================" << endl;
        cout << "\t\t*           用 户 界 面           *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*         (1) 财务信息管理        *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*         (2) 财务信息统计        *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*         (3) 数据管理            *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*         (4) 修改密码            *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*         (0) 退 出               *" << endl;
        cout << "\t\t===================================" << endl;
        cout << "\t\t请输入您的选择: ";
        cin >> select;
        switch (select)
        {
            case 0:
                break;
            case 1:
                managemenu(id);
                break;
            case 2:
                finance financeTemp;
                financeTemp.statistics(id);
                break;
            case 3:
                datamenu(id);
                break;
            case 4:
                alterpasswd(id);
                break;
            default:
                cout<<"\t\t输入错误请重新输入！"<<endl;
        }
    }
    while(select!=0);
    system("cls");
}
void managemenu(int id)    //财务信息管理界面
{
    int select=0;
    finance financeTemp;
    system("cls");
    do
    {
        cout << "\n\t\t===================================" << endl;
        cout << "\t\t*       财 务 管 理 界 面         *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*       (1) 增加财务信息          *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*       (2) 删除财务信息          *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*       (3) 查看财务信息          *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*       (4) 修改财务信息          *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*       (0) 退 出                 *" << endl;
        cout << "\t\t===================================" << endl;
        cout << "\t\t请输入您的选择: ";
        cin >> select;
        switch (select)
        {
            case 0:
                break;
            case 1:
                financeTemp.insertInfo(id);
                break;
            case 2:
                financeTemp.deleteInfo(id);
                break;
            case 3:
                financeTemp.lookmenu(id);
                break;
            case 4:
                financeTemp.updateInfo(id);
                break;
            default :
                system("cls");
                cout<<"\t\t输入错误请重新输入！"<<endl;
        }
    }
    while(select!=0);
    system("cls");
}
void datamenu(int id)   //数据管理界面
{
    int select=0;
    finance financeTemp;
    system("cls");
    do
    {
        cout << "\n\t\t===================================" << endl;
        cout << "\t\t*       数 据 管 理 界 面         *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*        (1) 备份数据             *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*        (2) 还原数据             *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*        (3) 导出数据             *" << endl;
        cout << "\t\t*                                 *" << endl;
        cout << "\t\t*        (0) 退 出                *" << endl;
        cout << "\t\t===================================" << endl;
        cout << "\t\t请输入您的选择: ";
        cin >> select;
        switch (select)
        {
            case 0:
                break;
            case 1:
                financeTemp.datamanage(1,id);
                break;
            case 2:
                financeTemp.datamanage(2,id);
                break;
            case 3:
                financeTemp.datamanage(3,id);
                break;
            default :
                system("cls");
                cout<<"\t\t输入错误请重新输入！"<<endl;
        }
    }
    while(select!=0);
    system("cls");
}
void alterpasswd(int id)     //修改密码函数
{
    MYSQL mysql;
    mysql_init(&mysql);
    if(mysql_real_connect(&mysql,"localhost","root","123456","financialManage",3306,NULL,0)!=NULL)
    {
        char str[100],originalpasswd[20],newpasswd1[20],newpasswd2[20];
        cout << "请输入原密码：";
        cin >> originalpasswd;
        cout << "请输入新密码：";
        cin >> newpasswd1;
        cout << "请再次输入新密码：";
        cin >> newpasswd2;
        system("cls");
        if(strcmp(newpasswd1,newpasswd2)==0)
        {
            mysql_query(&mysql,"SET NAMES GBK");
            sprintf(str,"update userInformation set passwd = '%s' where (id = %d and passwd = '%s')",newpasswd1,id,originalpasswd);
            if( mysql_real_query(&mysql,str,(unsigned int)strlen(str))==0)
                if(mysql_affected_rows(&mysql)!=0)
                    cout<<"修改成功!";
                else
                    cout<<"原密码错误或未做修改！";
            else
                cout<<"修改删除!";
        }
        else
            cout << "两次输入不一致！" << endl;
    }
    else
        cout << "数据库连接失败!" << endl;
    mysql_close(&mysql);
}
